﻿using EmoneyWeiXinCommon;
using EmoneyWeiXinEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EmoneyWeiXinBLL
{
    public class WeiXinApiBLL
    {

        static MemcachedTools1 mmtools = new MemcachedTools1();
        /// <summary>
        /// 获取在线客服接待信息
        /// </summary>
        /// <returns></returns>
        public WeiXin_kf_online_list Get_kf_online_list(string weixinId)
        {
            WeiXin_kf_online_list model = null;
            string ret = "";
            try
            {
                string accessToken = string.Empty;
                string mmkey = StringTools.ChangeRealKey("weixinUser", weixinId);
                weixinUserEntity weixinuser = JsonTools.ToObject<weixinUserEntity>(mmtools.Get(mmkey).ToString());
                accessToken = TokenHelper.GetToken(weixinuser.AppId, weixinuser.AppSecret);
                Dictionary<string, string> urlParams = new Dictionary<string, string>();
                // var tokek = "cA3DIFnR1UGJ6p7GxQOF-AwX4sbkcDJjEBXOL__Cpqqa6ZMx-wqLVK0apu12QgbMq_OwQNv2J4dkBij7YJKVKA";
                //urlParams.Add("access_token", tokek);
                urlParams.Add("access_token", accessToken);
                string url = Config.WeiXin_ApiUrl + "customservice/getonlinekflist";
                ret = NetHelper.LoadData(url, urlParams);
                if (ret.IndexOf("{\"errcode\":40001") != -1)
                {
                    TokenHelper.ClearToken();
                    urlParams = new Dictionary<string, string>();
                    urlParams.Add("access_token", TokenHelper.GetToken("", ""));
                    ret = NetHelper.LoadData(url, urlParams);
                }
                //var  ret1 = "{\"kf_online_list\": [{\"kf_account\": \"test1@test\",\"status\": 1,\"kf_id\": \"1001\",\"auto_accept\": 0,\"accepted_case\": 1},{\"kf_account\": \"test2@test\",\"status\": 1,\"kf_id\": \"1002\",\"auto_accept\": 10,\"accepted_case\": 2}]}";
                if (ret.IndexOf("{\"errcode\":") == -1 && !string.IsNullOrEmpty(ret))
                {
                    model = JsonTools.ToObject<WeiXin_kf_online_list>(ret);
                }
            }
            catch (Exception ex)
            {

            }
            return model;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="kf_id"></param>
        /// <returns></returns>
        public bool IsReachAccept(string kf_account, string weixinid)
        {
            var obj = Get_WeiXin_kf_online(kf_account, weixinid);
            if (obj != null)
            {
                var auto_accept = obj.auto_accept;
                var accepted_case = obj.accepted_case;
                if (accepted_case > auto_accept)
                    return true;//达到最大值
                else
                    return false;
            }
            else
            {
                return true;//如果为null也认为达到最大值，这样也可以连接到其他客服，不会由于接口繁忙错误导致客户一直等待某一个客服
            }
        }

        /// <summary>
        /// 根据kfid获取客服消息
        /// </summary>
        /// <param name="kfid"></param>
        /// <returns></returns>
        public WeiXin_kf_online Get_WeiXin_kf_online(string kf_account, string weixinId)
        {
            WeiXin_kf_online obj = null;
            WeiXin_kf_online_list list = this.Get_kf_online_list(weixinId);
            if (list != null)
            {
                List<WeiXin_kf_online> kf = list.kf_online_list;
                if (kf != null && kf.Count > 0)
                {
                    kf = kf.Where(p => p.kf_account == kf_account).ToList();
                    if (kf != null && kf.Count > 0)
                        obj = kf[0];
                }
            }
            return obj;
        }
    }
}
